﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DBF_ReadWrite;
using VectorAnalys;

namespace OpenShape
{
    public partial class frmIdentify : Form
    {
        Scaler m_scaler = new Scaler();
        DBF_FileReadWriter DBF_FileRW = new DBF_FileReadWriter();

        public event Deligate_IdentifyType frmIdentifyTypeChanged; //델리게이트

        public frmIdentify( )
        {
            InitializeComponent();
 
        }

        private void frmIdentify_Load(object sender, EventArgs e)
        {
            
        }

        private void frmIdentify_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            if(sender.Equals(this)){
              e.Cancel = true;

              Hide(); // 난 닫기 버튼 누를 때 그냥 폼을 감추고만 싶을 뿐이야~!!
            }

        }

        public void FeatureInfoShow(object objectSeleltFeatureID, PointF SelectLoc)
        {
            this.Show();
            trFeatureTree.Nodes.Clear();
            dgFeatureInfo.Rows.Clear();

            List<SelectFeatureID> pSelectFeatureID = new List<SelectFeatureID>();
            pSelectFeatureID = (List<SelectFeatureID>)objectSeleltFeatureID;

            if (pSelectFeatureID.Count > 0)
            {
                int temp_trStringIndex = 0;
                int temp_LayerIndex = 0;
                txtLocation.Text = SelectLoc.X + " , " + SelectLoc.Y + "  m";

                trFeatureTree.BeginUpdate();
                trFeatureTree.Nodes.Add("속성확인 개체");

                int LayerCount = 0;
                for (int i = 0; i < pSelectFeatureID.Count; i++)
                {
                    temp_LayerIndex = pSelectFeatureID[i].LayerID;
                    LayerManager.m_Layers[temp_LayerIndex].m_DBF = DBF_FileRW.Reader(LayerManager.m_Layers[temp_LayerIndex].m_DataSourceName);
                    for (int j = 0; j < LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Columns.Count; j++)
                    {
                        if (LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Columns[j].DataType == System.Type.GetType("System.String"))
                        {
                            temp_trStringIndex = j;
                            j = LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Columns.Count;
                        }
                    }

                    string Temp_LayerName = null;
                    string[] entries;
                    entries = LayerManager.m_Layers[temp_LayerIndex].m_DataSourceName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string entry in entries)
                    {
                        Temp_LayerName = entry;
                    }

                    if (LayerCount == 0)
                    {
                        trFeatureTree.Nodes[0].Nodes.Add(Temp_LayerName);
                        trFeatureTree.Nodes[0].Nodes[0].Nodes.Add
                            (LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Rows[pSelectFeatureID[i].FeatureID][temp_trStringIndex].ToString());
                        LayerCount += 1;
                    }
                    else
                    {
                        
                        if (trFeatureTree.Nodes[0].Nodes[LayerCount - 1].Text  == Temp_LayerName)
                        {
                            trFeatureTree.Nodes[0].Nodes[LayerCount-1].Nodes.Add
                                (LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Rows[pSelectFeatureID[i].FeatureID][temp_trStringIndex].ToString());

                        }
                        else
                        {
                            trFeatureTree.Nodes[0].Nodes.Add(Temp_LayerName);
                            trFeatureTree.Nodes[0].Nodes[LayerCount].Nodes.Add
                                (LayerManager.m_Layers[temp_LayerIndex].m_DBF.pDataTable.Rows[pSelectFeatureID[i].FeatureID][temp_trStringIndex].ToString());
                            LayerCount += 1;
                        }
                    }

                }
                trFeatureTree.EndUpdate();
                trFeatureTree.ExpandAll();

                for (int i = 0; i < LayerManager.m_Layers[pSelectFeatureID[0].LayerID].m_DBF.pDataTable.Columns.Count; i++)
                {
                    string[] temp_Array = new string[2];
                    temp_Array[0] = LayerManager.m_Layers[pSelectFeatureID[0].LayerID].m_DBF.pDataTable.Columns[i].ColumnName;
                    temp_Array[1] = LayerManager.m_Layers[pSelectFeatureID[0].LayerID].m_DBF.pDataTable.Rows[pSelectFeatureID[0].FeatureID][i].ToString();

                    dgFeatureInfo.Rows.Add(temp_Array);
                }
            }

        }

        public void cbIdentyType_SelectedIndexChanged(object sender, EventArgs e)
        {

            cbIdentyType.Text = cbIdentyType.SelectedText;
            frmIdentifyTypeChanged(cbIdentyType.SelectedIndex);
        }
    }
}
